<?php
require_once "src/apiClient.php";
require_once "src/contrib/apiCalendarService.php";
session_start();

$client = new apiClient();
$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
 $client->setClientId('770938131025.apps.googleusercontent.com');
$client->setClientSecret('bOih7XMjWC6E5M18eaGSV8EI');
 $client->setRedirectUri('http://localhost:8888/calendar/reponse.php');

$client->setDeveloperKey('AI39si6a39ey9Zl_elSjD3eTiJV7Aiav2FK3luNQweZGdvLckqPGINxRF349spYyrzESSINdA3kOQgQknnROREMKhzOtNg0Zvw');
$client->setUseObjects(true);
$cal = new apiCalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
	 header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
 $calList = $cal->calendarList->listCalendarList();
	print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

	
	$events = $cal->events->listEvents('wangxiaoming78@gmail.com');


//print_r($events);
/*
foreach ($events['items'] as $items )
{
	echo "id:".$items['id']."description:".$items['description']."<hr>";
	
}

while(true) {
  foreach ($events->getItems() as $event) {
    echo $event->getSummary();
  }
  $pageToken = $events->getNextPageToken();
  if ($pageToken) {
    $optParams = array('pageToken' => $pageToken);
    $events = $service->events->listEvents('primary', $optParams);
  } else {
    break;
  }
}
// insert event	
$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2012-09-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2012-09-03T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('wangxiaoming@hotmail');

$attendees = array($attendee1 );
$event->attendees = $attendees;
$createdEvent = $cal->events->insert('primary', $event);

echo $createdEvent->getId();	

 */	
	
// update event
	
	
	$_SESSION['token'] = $client->getAccessToken();
	
  
  



} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

?>